package net.hymnal.church;

import java.io.IOException;







import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HymnActivity  extends Activity {
	
	final int   PLAY_SOUND_MENU = 100;
	final int   PLAY_SOUND_MENU_TUNE = 101;
	final int 	MENU_ID = 1000;

	TextView    mHymnText;
	TextView    mHymnTextNumer;
	SingleHymn 	mHymn;
	Button		mPrevButton;
	Button 		mNextButton;
	Button 		mMinusButton;
	Button		mPlusButton;
	LinearLayout mLayout;
	
	static  boolean BLACK = false;	
	static  private int selectedHymn = 0;

	MediaPlayer mediaPlayer = new MediaPlayer();
	
	static public void setSelectedHymn(int num) {
        selectedHymn = num;
	}
	
	OnClickListener mButtonClickListener = new View.OnClickListener(){

		public void onClick(View view) {
			
			if(view == mPrevButton){
				if (selectedHymn > 0) {
					stopMediaPlayer();
					selectedHymn -= 1;
					loadHymnText();
				}				
				return;
			}
			
			if (view == mNextButton) {
				int maxHymns = 0;

				maxHymns = HymnsManager.getNumberHymns();
				if (selectedHymn < (maxHymns - 1)) {
					stopMediaPlayer();
					selectedHymn += 1;
					loadHymnText();
				}
				return;
			}
			if(view == mMinusButton){
				decTextSize();
				return;
			}
			if(view == mPlusButton){
				incTextSize();
				return;
			}				
		}		
	};
	ScrollingMovementMethod mScrollingMethod = new ScrollingMovementMethod();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(GimnsMain.mSettings.getCurrentTheme());
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.hymn);
		
		initControls();
		loadHymnText();
		
	}
	void initControls() {
		int bgColor = -1;
		int bgColorTitle = -1;
		int textColorNumber = -1;
		
		mHymnText = (TextView)findViewById(R.id.hymntext);		
		mHymnText.setTextSize((int)(GimnsMain.mSettings.mTextSize));
		mHymnText.setMovementMethod(ScrollingMovementMethod.getInstance());
		mHymnText.setClickable(false);
		mHymnText.setLongClickable(false);
		
		mLayout = (LinearLayout)findViewById(R.id.butonLayout);
		if(BLACK) {
		    bgColor = Settings.BackgoundColor.BACK_GROUND_BLACK;
		    bgColorTitle = Settings.BackgoundColor.BACK_GROUND_BLACK_TITLE;
		    textColorNumber = Color.BLUE;
		} else {
			bgColor = Settings.BackgoundColor.BACK_GROUND_WHITE;			
			bgColorTitle = Settings.BackgoundColor.BACK_GROUND_WHITE_TITLE;
			textColorNumber = Color.LTGRAY;
		}
		mLayout.setBackgroundColor(bgColor);
		
		mHymnTextNumer = (TextView)findViewById(R.id.hymntext_number);
		mHymnTextNumer.setTextSize((int)(GimnsMain.mSettings.mTextSize));
		mHymnTextNumer.setClickable(false);
		mHymnTextNumer.setLongClickable(false);
		mHymnTextNumer.setBackgroundColor(bgColorTitle);
		mHymnTextNumer.setTextColor(textColorNumber);		
		mHymnTextNumer.setTextSize(Settings.getDefualtTextSize(this));
		
		mMinusButton = (Button)findViewById(R.id.btDec);	
		mMinusButton.setOnClickListener(mButtonClickListener);
	    mMinusButton.setText("-");
		mMinusButton.setTextSize(Settings.buttonTextSize);
		
		mPlusButton = (Button)findViewById(R.id.btInc);		
		mPlusButton.setOnClickListener(mButtonClickListener);
		mPlusButton.setText("+");
		mPlusButton.setTextSize(Settings.buttonTextSize);
		
		if(HymnsManager.isSelected()){
			return;
		}
		
		mPrevButton = (Button)findViewById(R.id.btPrev);
		mPrevButton.setOnClickListener(mButtonClickListener);
		mPrevButton.setText("<");
		mPrevButton.setTextSize(Settings.buttonTextSize);
		
		
		mNextButton = (Button)findViewById(R.id.btNext);
		mNextButton.setOnClickListener(mButtonClickListener);
		mNextButton.setText(">");
		mNextButton.setTextSize(Settings.buttonTextSize);		
	}

	void loadHymnText() {
		mHymn = HymnsManager.getSingleHymn(selectedHymn);
		HymnsManager.setLastSelectedHymn(HymnsManager.getSelectedType(), selectedHymn);
		
		mHymnText.setText(mHymn.getText());		
		mHymnTextNumer.setText(Integer.toString(mHymn.getNumber()));
	}

	void incTextSize() {
		int oldTextSize = GimnsMain.mSettings.mTextSize;
		int newTextSize = oldTextSize;
		if (oldTextSize < Settings.TextSettings.MAX_TEXT_HYMN_SIZE) {
			newTextSize = (byte) (newTextSize + 1);
		}
		mHymnText.setTextSize(newTextSize);
		//mHymnTextNumer.setTextSize(newTextSize);
		GimnsMain.mSettings.mTextSize = newTextSize;
	}

	void decTextSize() {
		int oldTextSize = GimnsMain.mSettings.mTextSize;
		int newTextSize = oldTextSize;
		if (oldTextSize > Settings.TextSettings.MIN_TEXT_HYMN_SIZE) {
			newTextSize = (byte) (newTextSize - 1);
		}		
		mHymnText.setTextSize(newTextSize);
		//mHymnTextNumer.setTextSize(newTextSize);
		GimnsMain.mSettings.mTextSize = newTextSize;
	}
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	final int itemID = item.getItemId();
    	stopMediaPlayer();
    	String filename = null;
    	AssetFileDescriptor descriptor;
    	int englishNumber = mHymn.getEnglishNumber();
		if (englishNumber > 0) {
			if (itemID == PLAY_SOUND_MENU) {
				filename = Settings.PathesClass.commonHymnalMelodyPath
						+ mHymn.getMelodyFileName();
			}
			if (itemID == PLAY_SOUND_MENU_TUNE) {
				filename = Settings.PathesClass.commonHymnalTuneMelodyPath
						+ mHymn.getTuneMelodyFileName();
			}
		} else {
			return true;
		}
		
        try {
            descriptor = getAssets().openFd(filename);				    
			mediaPlayer.reset();
    		mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());					
	    	mediaPlayer.prepare();
		    mediaPlayer.start();
		} catch (IOException e) {					
			System.out.println("IOException:" + e.getMessage());
		} catch (IllegalArgumentException iae) {
			System.out.println("ie:" + iae.getMessage());
		} catch (SecurityException se) {
			System.out.println("se" + se.getMessage());
		} catch (IllegalStateException ise) {
			System.out.println("ise" + ise.getMessage());
		}
		return true;	     
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		String fname = mHymn.getMelodyFileName(); 
		if((fname == null) || (fname == "")) {
			return false;
		}
		int engNum = mHymn.getEnglishNumber();
		if(engNum <= 0) {
			return false;
		}		
	    menu.add(0, PLAY_SOUND_MENU, 0, getString(R.string.media_play));
	    menu.add(0, PLAY_SOUND_MENU_TUNE, 0, getString(R.string.media_play_tune));
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(mediaPlayer.isPlaying()) {
            	mediaPlayer.stop();
            }
            finish();
	        return true;
	    }
	    return false;	    
	}
	
	void stopMediaPlayer() {
		if(mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
		}
	}
	
	static void startHymnActivity(Activity owner, View v, int number) {
		selectedHymn = number;
		Intent myIntent = new Intent(v.getContext(),
				HymnActivity.class);
		owner.startActivityForResult(myIntent, 0);
	}
}
